package com.cbgolf.oa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.CouponBean;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.DensityUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.TypeUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.widget.AutoResizeTextView;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lq on 2017/9/4.
 */

public class CouponAdapter extends BaseAdapter {
    private List<CouponBean> list;
    private LayoutInflater mInflater;
    private onCouponClickListener listener;
    private Context context;

    public CouponAdapter(Context context, onCouponClickListener listener) {
        mInflater = LayoutInflater.from(context);
        list = new ArrayList<>();
        this.listener = listener;
        this.context = context;
    }

    public void clear() {
        if (null != this.list && this.list.size() > 0) {
            this.list.clear();
        }
    }

    public void setList(List<CouponBean> list) {
        this.list = list;
    }


    public void addData(List<CouponBean> list) {
        this.list.addAll(list);
    }

    public void upDataUI() {
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder h = null;
        if (null == view) {
            h = new Holder();
            view = mInflater.inflate(R.layout.item_coupon, null);
            h.dateTv = (TextView) view.findViewById(R.id.item_coupon_time_tv);
            h.numTv = (AutoResizeTextView) view.findViewById(R.id.item_coupon_num_tv);

            h.parkNameTv = (TextView) view.findViewById(R.id.item_coupon_name_tv);
            h.typeTv = (TextView) view.findViewById(R.id.item_coupon_type_tv);
            h.lowestTv = (TextView) view.findViewById(R.id.item_coupon_limit_tv);
            h.useTimeTv = (TextView) view.findViewById(R.id.item_coupon_duration_tv);
            h.stateTv = (TextView) view.findViewById(R.id.item_coupon_state_tv);
            h.delTv = (TextView) view.findViewById(R.id.item_coupon_del_tv);
            h.pushTv = (TextView) view.findViewById(R.id.item_coupon_push_tv);
            h.yesOrNoView = view.findViewById(R.id.item_coupon_yes_orno_ll);
            h.disabledTv = (TextView) view.findViewById(R.id.item_coupon_disabled_tv);
            AutoUtil.auto(view);
            view.setTag(h);
        } else {
            h = (Holder) view.getTag();
        }
        CouponBean m = list.get(position);
        if (null != m) {
            String date = m.pushDate;
            h.dateTv.setText("发放时间  " + DateUtil.stampToDate(date, DateUtil.formatWithbias));
            String moneyNum = m.parValue;
            if (!Util.isNull(moneyNum)) {
                try {
                    BigDecimal bd = new BigDecimal(moneyNum);
                    double d = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    DecimalFormat df = new DecimalFormat("#.##");
                    moneyNum = df.format(d);
                } catch (Exception e) {

                }
            }
            h.numTv.resetSize(DensityUtil.sp2px(context, 40));
            h.numTv.setText(Util.isNull(moneyNum) ? "" : moneyNum);

            String couponType = EnumUtil.getCouponType(m.couponType);
            h.typeTv.setText(couponType);
            String parkName = m.courseName;
            if (Util.isNull(parkName)) {
                parkName = "";
            }
            h.parkNameTv.setText(parkName);
            int low = m.lowestConsumption;
            h.lowestTv.setText("1.订场满" + low + "元可使用");
            String endTime = "";
            String startTime = "";
            if (null != m.availabilityPeriod) {
                startTime = m.availabilityPeriod.start;
                endTime = m.availabilityPeriod.end;
            }
            if (TypeUtil.parseLong(endTime) > System.currentTimeMillis()) {
                boolean pushed = m.generated;
                if (pushed) {
                    String isNoDisable = m.isReceivable;
                    int allNum = m.quantity;
                    int outNum = m.receivedQuantity;
                    if (0 != allNum && allNum == outNum) {
                        h.stateTv.setText("已领完");
                        h.yesOrNoView.setVisibility(View.GONE);
                        h.disabledTv.setVisibility(View.GONE);
                    } else if (Util.isNull(isNoDisable) || Util.isEquals(false, isNoDisable)) {
                        h.stateTv.setText("已禁用");
                        h.yesOrNoView.setVisibility(View.GONE);
                        h.disabledTv.setVisibility(View.GONE);
                    } else {
                        h.stateTv.setText("已发布");
                        h.yesOrNoView.setVisibility(View.GONE);
                        h.disabledTv.setVisibility(View.VISIBLE);
                    }
                } else {
                    h.stateTv.setText("未发布");
                    h.yesOrNoView.setVisibility(View.VISIBLE);
                    h.disabledTv.setVisibility(View.GONE);
                }
            } else {
                h.stateTv.setText("已过期");
                h.yesOrNoView.setVisibility(View.GONE);
                h.disabledTv.setVisibility(View.GONE);
            }
            startTime = DateUtil.stampToDateyMd2(startTime);
            endTime = DateUtil.stampToDateyMd2(endTime);
            h.useTimeTv.setText("2.有效期" + startTime + "-" + endTime);
            h.delTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onDel(position);
                    }
                }
            });
            h.pushTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onPush(position);
                    }
                }
            });
            h.disabledTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onDisable(position);
                    }
                }
            });
        }
        return view;
    }

    class Holder {
        private TextView dateTv;
        private AutoResizeTextView numTv;
        private TextView typeTv;
        private TextView parkNameTv;
        private TextView lowestTv;
        private TextView useTimeTv;
        private TextView stateTv;
        private TextView delTv;
        private TextView pushTv;
        private TextView disabledTv;
        private View yesOrNoView;

    }

    public interface onCouponClickListener {
        void onDel(int position);

        void onPush(int position);

        void onDisable(int position);
    }
}
